package org.oschina.api.favorite.type.service;

import java.util.List;
import org.oschina.Message;
import org.oschina.Module;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.api.favorite.type.dao.FavoriteTypeDAO;
import org.oschina.api.favorite.type.model.FavoriteType;
import org.oschina.api.favorite.type.vo.FavoriteTypeVo;
import org.oschina.bean.Result;
import org.oschina.security.ISecurity;
import org.oschina.security.annotation.Require;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FavoriteTypeService extends BaseService {

	@Autowired
	private FavoriteTypeDAO favoriteTypeDAO;
	@Autowired
	private ISecurity security;
	
	/**
	 * 返回
	 * @return
	 */
	public List<FavoriteTypeVo> findVo()
	{
		List<FavoriteTypeVo> result=_findVo();
		if(!security.require(new String[]{Module.MODULE_53}, getToken(), false))
		{
			for(FavoriteTypeVo favoriteTypeVo:result)
				favoriteTypeVo.setUrl(null);
		}
		return result;
	}
	
	/**
	 * 更新
	 * @param favoriteType
	 * @return
	 */
	@Require(authorization=Module.MODULE_53)
	public Result update(FavoriteType favoriteType)
	{
		if(_update(favoriteType)==0)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		return ResultUtil.getSuccess(favoriteType);
	}

	/**
	 * 返回
	 * @return
	 */
	private List<FavoriteTypeVo> _findVo()
	{
		return favoriteTypeDAO.findVo();
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public FavoriteTypeVo _findVoById(int id)
	{
		return favoriteTypeDAO.findVoById(id);
	}
	
	/**
	 * 更新
	 * @param favoriteType
	 * @return
	 */
	private int _update(FavoriteType favoriteType)
	{
		return favoriteTypeDAO.update(favoriteType);
	}
}